package com.icehand.payment.api.v1.service.impl;

import com.icehand.common.code.OrderCode;
import com.icehand.common.constants.RedisKeyConstants;
import com.icehand.common.enums.PayChannelEnum;
import com.icehand.common.model.BaseOrder;
import com.icehand.common.model.BaseUser;
import com.icehand.common.result.BizResult;
import com.icehand.payment.api.v1.service.PayChannelServiceV1;
import com.icehand.payment.api.v1.service.PayServiceV1;
import com.icehand.payment.model.NotifyWrapper;
import com.icehand.payment.model.PayRequest;
import com.icehand.payment.model.PayWrapper;
import com.icehand.payment.pay.PayTemplate;
import lombok.extern.slf4j.Slf4j;
import org.springframework.amqp.rabbit.core.RabbitTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import java.math.BigDecimal;

/**
 * @author icehand
 */
@Service
@Slf4j
public class PayServiceImplV1 implements PayServiceV1 {
    private final PayChannelServiceV1 payChannelServiceV1;
    private final RedisTemplate redisTemplate;
    private final PayTemplate payTemplate;
    private final RabbitTemplate rabbitTemplate;

    @Autowired
    public PayServiceImplV1(PayChannelServiceV1 payChannelServiceV1, RedisTemplate redisTemplate,
                            PayTemplate payTemplate, RabbitTemplate rabbitTemplate) {
        this.payChannelServiceV1 = payChannelServiceV1;
        this.redisTemplate = redisTemplate;
        this.payTemplate = payTemplate;
        this.rabbitTemplate = rabbitTemplate;
    }

    @Override
    public BizResult pay(PayRequest payRequest, BaseUser user) {
        //redis中校验订单号
        BaseOrder order = (BaseOrder) redisTemplate.opsForValue().get(RedisKeyConstants.ORDER_PREFIX +
                payRequest.getAppId()+ ":"+ payRequest.getOrderNo());
        if(null == order) {
            return new BizResult<>(OrderCode.NOT_FOUND);
        }

        if(BigDecimal.ZERO.compareTo(order.getAmount())>-1){
            return new BizResult<>(OrderCode.ERROR);
        }

        PayChannelEnum payChannelEnum = PayChannelEnum.choiceByChannelId(payRequest.getChannelId());
        PayWrapper payWrapper = new PayWrapper()
                .setPayChannelEnum(payChannelEnum)
                .setConfig(payChannelServiceV1.payChannelConfig(payRequest.getAppId(),payRequest.getChannelId()))
                .setBaseOrder(order)
                .setBizResult(new BizResult<>())
                ;
        payTemplate.pay(payWrapper);
        return payWrapper.getBizResult();
    }

    @Override
    public String payNotify(Integer channelId, Integer appId, HttpServletRequest request) {
        PayChannelEnum payChannelEnum = PayChannelEnum.choiceByChannelId(channelId);
        NotifyWrapper wrapper = new NotifyWrapper()
                .setPayChannelEnum(payChannelEnum)
                .setAppId(appId)
                .setConfig(payChannelServiceV1.payChannelConfig(appId,channelId))
                .setRequest(request)
                .setBizResult(new BizResult<>());
        payTemplate.payNotify(wrapper);

        if(!wrapper.getBizResult().isSuccess()){
            return wrapper.getPayChannelEnum().getPayNotifyResultEnum().getFail();
        }

        //DONE:发送消息，直接在订单部分
        BaseOrder baseOrder = wrapper.getBizResult().getData();
        rabbitTemplate.convertAndSend("order-notify-"+baseOrder.getOrderNo(), baseOrder);
        return wrapper.getPayChannelEnum().getPayNotifyResultEnum().getSuccess();
    }
}
